$(function () {
    getUserInfo();

    // // 退出
    $('#logout').on('click', function () {
        layer.confirm('确认退出吗!', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html';
            layer.close(index)
        })
    })
});


function getUserInfo() {
    axios({

        url: "/my/userinfo",
        headers: {
            Authorization: localStorage.getItem('token')
        }
    }).then(res => {
        // console.log(res.data);
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        renderAvatar(res.data.data)
    })

}
// 渲染用户及头像
function renderAvatar(user) {
    let name = user.nickname || user.username;
    $('.welcome').html("欢迎 " + name);
    if (user.user_pic !== null) {
        // 有图片头像就显示图片头像，隐藏文字头像
        $('.avatar-text').hide();
        $('.layui-nav-img').show().attr('src', user.user_pic)
    } else {
        // 没有图片头像就隐藏图片头像，显示文字头像
        $('.layui-nav-img').hide();
        let first = name[0].toUpperCase();
        $('.avatar-text').show().html(first)
    }
}
